<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */ 
namespace osc\member\controller;
use osc\common\controller\MemberBase;
use think\Db;
class OrderMember extends MemberBase{
	
	protected function _initialize(){
		parent::_initialize();		
		$this->assign('breadcrumb','订单管理');		
	}
	
	//取得订单列表
	function get_order_list(){
		$this->assign('status',Db::name('order_status')->select());		
		$data=osc_order()->member_order_lists(input('param.'),10,UID);		
		$this->assign('list',$data['page']);
		$this->assign('data',$data['data']);
		$this->assign('empty','<div id="empty">没有数据</div>');
		$this->assign('goods_type',osc_goods()->get_goods_type());		
		//待付款
		$this->assign('no_pay',Db::name('order')->where(array('order_status_id'=>config('default_order_status_id'),'uid'=>UID))->count());
		//待确认收货(已经发货的)
		$this->assign('no_get',Db::name('order')->where(array('order_status_id'=>config('shipping_order_status_id'),'uid'=>UID))->count());
		//待评价(已经收货的)
		$this->assign('no_reply',Db::name('order')->where(array('order_status_id'=>config('get_order_status_id'),'uid'=>UID))->count());
		$this->assign('hashids',hashids());
		$this->assign('SEO',array('title'=>'订单管理-'.config('SITE_TITLE')));
	}
	
	function index(){
		$this->get_order_list();
		return $this->fetch();
	}
	//订单类型
	function order_type(){
		$this->get_order_list();
		return $this->fetch('index');
	}
	//订单状态
	function status(){
		$this->get_order_list();
		return $this->fetch('index');
	}
	//订单详情
	public function show_order(){
			
     	$id=hashids_decode(input('param.id'));
		
		if(!$id){
			$this->error('非法操作');
		}
		
		$order=osc_order()->order_info($id,UID);
				
		storage_user_action(UID,member('username'),config('FRONTEND_USER'),'查看了订单详情');
		$this->assign('data',$order);		
		$this->assign('crumbs','订单详情');
		$this->assign('SEO',array('title'=>'订单详情-'.config('SITE_TITLE')));		
    	return $this->fetch('show');
	 }
	//物流信息
	function get_trace_info(){
		
		$id=hashids_decode(input('param.order_id'));
		
		if(!$id){
			$this->error('非法操作');
		}
		
		return osc_service('common','kuaidi')->get_trace_info($id);
	}
	
	//订单历史
	 public function history(){
			
			$id=hashids_decode(input('param.id'));
			
			$results = osc_order()->get_order_histories($id,UID);
		
			foreach ($results as $result) {
				$histories[] = array(
					'notify'     => $result['notify'] ? '是' : '否',
					'status'     => $result['name'],
					'comment'    => nl2br($result['comment']),
					'date_added' => date('Y/m/d H:i:s', $result['date_added'])
				);
			}

			$this->assign('histories',$histories);	
			
			exit($this->fetch());
	}
	 //取消订单
	function cancel(){
		
		$id=hashids_decode(input('param.id'));
		
		if(!$id){
			$this->error('非法操作');
		}
						
		osc_order()->cancel_order($id,UID);
		storage_user_action(UID,member('username'),config('FRONTEND_USER'),'取消了订单');
		$this->success('取消成功',url('OrderMember/index'));
	}
	//确认收货
	function confirm(){
		$id=hashids_decode(input('param.id'));
		
		if(!$id){
			$this->error('非法操作');
		}
						
		osc_order()->confirm_order($id,UID);
		
		storage_user_action(UID,member('username'),config('FRONTEND_USER'),'确认收货');
		
		$this->success('确认成功',url('OrderMember/index'));
	}
}
